﻿using App.Desk.Extend;
using App.Desk.UserControls;
using App.Untiys;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.Desk.Pro.Monitors
{
    public partial class FrmProtectState : UIPage
    {
        public List<PLCParaModel> plcParas = new List<PLCParaModel>();
        public FrmProtectState()
        {
            InitializeComponent();
            SetInfo();
            SamplingPLCHelper.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }
        public void SetInfo()
        {
            List<string> paraList = new List<string>();

            paraList.Add("大车计数|V|42.0");
            paraList.Add("大车前限位|V|42.1");
            paraList.Add("大车后限位|V|42.2");
            paraList.Add("小车左限位|V|42.3");
            paraList.Add("小车右限位|V|42.5");
            paraList.Add("车厢检测|V|42.6");
            paraList.Add("小车计数|V|43.1");
            paraList.Add("升降计数|V|43.2");
            paraList.Add("允许移动大小车|V|43.3");
            paraList.Add("升降上限位|V|43.4");
            paraList.Add("升降校正|V|43.5");
            paraList.Add("升降下限位|V|43.6");
            paraList.Add("允许下降|V|43.7");
            paraList.Add("集料斗开到位|V|44.0");
            paraList.Add("集料斗关到位|V|44.1");
            paraList.Add("给料机摆开位|V|44.2");
            paraList.Add("给料机接料位|V|44.3");
            paraList.Add("缩分原位|V|44.4");
            paraList.Add("集样器计数|V|44.6");
            paraList.Add("集样器原位|V|45.0");
            paraList.Add("弃料门开到位|V|45.1");
            paraList.Add("弃料门关到位|V|45.2");
             
            paraList.ForEach(res =>
            {
                var array = res.ToSplitArrayEx('|');
                plcParas.Add(new PLCParaModel
                {
                    Name = array[0],
                    Region = array[1],
                    Addr = array[2],
                });
            });
        }

        private void FrmProtectState_Load(object sender, EventArgs e)
        {

            UcStatusButton ucStatus;
            foreach (var item in plcParas)
            {
                ucStatus = new UcStatusButton();
                ucStatus.SetStyle(UIShape.Square);
                ucStatus.Width = 140;
                ucStatus.SetState(UILightState.Off);
                ucStatus.SetText(item.Name);
                ucStatus.Parameter = item;
                flowBox.Add(ucStatus);
            }
        }

        private void OnPlcDataReadOut(object send, ActiveReadEventArgs e)
        {
            short[] _arrReads = (short[])e.E_ReadValue;

            foreach (var control in flowBox.GetAllControls())
            {
                if (control is UcStatusButton)
                {
                    try
                    {
                        bool result = false;
                        var item = ((UcStatusButton)control);
                        if (item.Parameter.Region == "V")
                        {
                            int[] _aryBits;
                            var ints = item.Parameter.Addr.To_PLC_V_SPLIT();
                            if (ints.IsNotNull())
                            {
                                _aryBits = DataConvert.Ushort2Binary((int)(_arrReads[ints[0] / 2]));
                                result = _aryBits[ints[1]] == 1;
                            }
                        }
                        if (item.Parameter.Region == "VW")
                        {
                            result = _arrReads[(item.Parameter.Addr.ToPLCIntEx()) / 2].ToPLCIntEx() == 1;
                        }
                        UIHelper.SetLightControl((UcStatusButton)control, result ? UILightState.On : UILightState.Off);
                    }
                    catch
                    {

                    }
                }
            }
        }

    }
}
